﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;


namespace CMS
{
    public class listOfUsers : ResponseToken{
        public List<string> userz;

        public listOfUsers() {
            userz = new List<string>();
        }

        public listOfUsers(int code):  base(code){
            userz = new List<string>();
        }

        public listOfUsers(int code, List<string> usz)
            : base(code) {
            userz = usz;
        }

        

    }

    public class User {
        //public int userID;            //user's ID in database
        public string username;         //user's username  
        public string domain;           //User's domain (if auth is LDAP)
        public string password;         //password, as md5 hash        

        public serviceGroup serviceGR;   //admin or user
        public cmsRoleGroup[] cmsRoles;  //Change management role - CM, AB, CI
    }

    public enum cmsRoleGroup { CI, CM, AB, CIM, EM };
    public enum serviceGroup { USER, ADMIN };


    public class AuthentificatedUser : User
    {   
        //parameters only MCMS Servise should know about
        string hostname;                //hostname, user initiated login from 
        
        DateTime loginTime;             //Checks when user logged in
        DateTime lastInteraction;
                
        string sessionID;               //unique session ID, given after authorisation
                
        public AuthentificatedUser()
        {
            loginTime = new DateTime();
            loginTime = DateTime.Now;                        
            lastInteraction = loginTime;            
            sessionID = getMd5Hash( loginTime.ToString() + username);                        
        }

        // Hash an input string and return the hash as
        // a 32 character hexadecimal string.
        private string getMd5Hash(string input) {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++) {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        

        public DateTime LastInteraction
        {
            get { return lastInteraction; }
        }

        public string UserName{
            get {return username;}
            set { username = value.Trim(); } 
        }
        
        public string HostName {
            get { return hostname; }
            set { hostname = value; }
        }

        public string SessionID{
            get { return sessionID; }
            set { sessionID = value; }
        }

        public void Interact()
        {
            lastInteraction = DateTime.Now;
        }
    }




    //describes an instance of web interface, permitted to communicate with a service
    public class TrustedProxy {
        string proxyID;
        string hostname;        
    }


    //describes change request. 
    public class ChangeRequest {
        public long ID;
        public int initiator;
        public string request;
        public int? cm;
        public string impactEstim;
        public string costEstim;
        public bool isEvaluated;
        public DateTime implementation;
        public int? cim;
        public bool isClosed;
        public bool isImplemented;

        public ChangeRequest(string ci, string request) {

        }

        public ChangeRequest() { 
        
        }
    }

    public class CRArray {

        public List<ChangeRequest> list;

        public CRArray() {
            list = new List<ChangeRequest>();

        }        

        public void addCRToArray(ChangeRequest cr) {
            list.Add(cr);
        }

    }

    public class CRWithDependencies  {
        public CRArray CRRequested;
        public ComputerArray ComputersAffected;
        public OSArray OperatingSystemsAffected;
        public SoftwareArray SoftwaresAffected;
        public PSArray PSAffected;
        public PTArray PTAffected;

        public CRWithDependencies() { }
    }

    public class CRWithApproves {
        public CRArray CRRequested;
        public ApproveArray approves;

        public CRWithApproves() {
            CRRequested = new CRArray();
            approves = new ApproveArray();
        }
    }

    public class ApproveArray {
        public List<Approve> approve;
        public ApproveArray() {
            approve = new List<Approve>();
        }
        public void addNew(Approve appr) {
            approve.Add(appr);
        }
    }

    public class Approve {
        public long ID;
        public long UserID;
        public long ApprovedCR;
        public bool isApproved;
        public string comment;
        public DateTime approveDate;
    }
    
    public class APIAccessToken 
    {
        public string username;
        public string hostname;
        public string sessionID;

        public APIAccessToken()
        {
            
        }

        public APIAccessToken(string un, string hn, string sid)
        {
            username = un;
            hostname = hn;
            sessionID = sid;
        }
    }

    //never used
    public class LoginToken 
    {
        public string proxyID;
        public string username;
        public string hostname;
        public string Password;
    }

    /*
     * class, describes common properties of any config, stored in CMDB
     */
    public class Config {
        public long ID;
        public long softwareID;
        public long? prevVerID;        
        public string comment;
        public string filesysLoc;
    
        public Config(){
        }           

        public Config(long softid, string _comment, string location){
            softwareID = softid;
            comment = _comment;
            filesysLoc = location;
        }

        
        public Config(long softid, long prevID,  string _comment, string location){
            softwareID = softid;
            prevVerID = prevID;
            
            comment = _comment;
            filesysLoc = location;
        }
    }

    /*
     * Plain text
     */
    public class PlainTextConfig : Config {

        public string configText;

        PlainTextConfig(long softID, string config, string comment, string location)
            : base(softID, comment, location) {

            configText = config;
        }

        PlainTextConfig(long softid, string text, long prevID, string comment, string location)
            : base(softid, prevID, comment, location) {
            configText = text;
        }

        public PlainTextConfig() {
        }
    }

    public class PTArray{
        public List<PlainTextConfig> PTlist;
        public PTArray(){
            PTlist = new List<PlainTextConfig>();
        }
        public void addNew(PlainTextConfig pt){
            PTlist.Add(pt);
        }
    }


    /*
     * Partial State
     */
    public class PartialStateConfig : Config {
        public string scriptApplied;
        public string stateAferScript;

        PartialStateConfig(long softID, string script, string state, string comment, string location)
            : base(softID, comment, location) {

            scriptApplied = script;
            stateAferScript = state;
        }

        PartialStateConfig(long softID, string script, string state, long prevID, string comment, string location)
            : base(softID, prevID, comment, location) {

            scriptApplied = script;
            stateAferScript = state;
        }

        public PartialStateConfig() { }
    }


    public class PSArray {
        public List<PartialStateConfig> PSlist;
        public PSArray() {
            PSlist = new List<PartialStateConfig>();
        }
        public void addNew(PartialStateConfig ps) {
            PSlist.Add(ps);
        }
    }

    /*
     * License
     */
    public class license {
        public long? ID;
        public string licNO;
        public DateTime expDate;    //nullable
        public long softType;
        public int amount;

        public license() { }

        license(long id, long softid, string licno, int _amount) {
            ID = id;
            softType = softid;
            licNO = licno;
            amount = _amount;
        }

        license(long id, long softid, string licno, DateTime expiry, int _amount) {
            ID = id;
            softType = softid;
            expDate = expiry;
            licNO = licno;
            amount = _amount;
        }
    }

    public class licenseArray {
        public List<license> licenses;

        public licenseArray() {
            licenses = new List<license>();
        }

        public void addNew(license lic) {
            licenses.Add(lic);
        }
    }

    public class SoftType {
        public string SoftName;
        public string SoftVer;
        public SoftType() { }
    }

    /*
     * describes computer
     */
    public class Computer {
        public string ID;
        public string location;
        public long? userID;        //nullable
        public DateTime dateBought;
        public string comment;      //nullabe
        public string hwdescription;//nulable

        public Computer() { }
    }
    public class ComputerArray{
        public List<Computer> Computers;
        public ComputerArray(){
            Computers = new List<Computer>();
        }
        public void addNew(Computer comp){
            Computers.Add(comp);
        }
    }

    /*
     * describes software
     */
    public class Software {
        public long ID;
        public long osID;
        public long softTypeID;
        public long? prevVerID;
        public long? licID;

        public Software() { }
    }

    public class SoftwareArray{
        public List<Software> Softwares;
        public SoftwareArray () {
            Softwares = new List<Software>();
        }
        public void addNew(Software soft){
            Softwares.Add(soft);
        }
    }
    /*
     * describes operating system
     */
    public class OperatingSystem {
        public long ID;
        public string compID;
        public string hostname; //can be null
        public long softType;
        public long? licenseID;

        public OperatingSystem() { }
    }


    public class OSArray {
        public List<OperatingSystem> OperatingSystems;
        public OSArray() {
            OperatingSystems = new List<OperatingSystem>();
        }
        public void addNew(OperatingSystem os) {
            OperatingSystems.Add(os);
        }
    }
    
}
